﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace NutriLeaf.Web.Core.Message
{
    public class Message:IMessage
    {
        public void SendMessage(string messageText, int sentUserId, int receivedUserId, ref Model.NutriLeafDBEntities context)
        {
            Model.Message msg = new Model.Message();
            msg.MessageText = messageText;
            msg.SentUserID = sentUserId;
            msg.RecievedUserID = receivedUserId;
            context.Messages.AddObject(msg);
            context.SaveChanges();
        }

        public void ReplyToMessage(string messageText, int sentUserId, int receivedUserId, int replyMessageId, ref Model.NutriLeafDBEntities context)
        {
            //doubt
            Model.Message message = new Model.Message();
            if (context.Messages.Where(p => p.MessageID == replyMessageId).Count() != 0) {
                Model.Message msg = new Model.Message();
          
            }
        }

        public bool IsMessageRead(int messageId, ref Model.NutriLeafDBEntities context)
        {
            if (context.Messages.Where(p => p.MessageID == messageId).Count() != 0)
            {
                return context.Messages.Where(p => p.MessageID == messageId).First().MessageRead.Value;
            }
            else
                return false;
        }

        public void MarkMessageAsRead(int messageId, ref Model.NutriLeafDBEntities context)
        {
            throw new NotImplementedException();
        }

        public void MarkMessageAsRead(Model.Message message, ref Model.NutriLeafDBEntities context)
        {
            //requires update
            throw new NotImplementedException();
        }

        public void MarkMessagesAsRead(List<int> messageIds, ref Model.NutriLeafDBEntities context)
        {
            //requires update
            throw new NotImplementedException();
        }

        public void MarkMessagesAsRead(List<Model.Message> messages, ref Model.NutriLeafDBEntities context)
        {
            //requires update
            throw new NotImplementedException();
        }

        public Model.Message GetMessage(int messageId, ref Model.NutriLeafDBEntities context)
        {
            return context.Messages.Where(p => p.MessageID==messageId).First();
          
        }

        public List<Model.Message> GetMessageThread(int messageId, ref Model.NutriLeafDBEntities context)
        {
            throw new NotImplementedException();
        }

        public List<Model.Message> GetAllMessagesForThisUser(int sendUserId, int receivedUserId, ref Model.NutriLeafDBEntities context)
        {
            return context.Messages.Where(p => p.SentUserID == sendUserId && p.RecievedUserID == receivedUserId).ToList();
        }

        public List<Model.Message> GetSentMessages(int userId, ref Model.NutriLeafDBEntities context)
        {
            return context.Messages.Where(p => p.SentUserID == userId).ToList();
        }

        public List<Model.Message> GetReceivedMessages(int userId, ref Model.NutriLeafDBEntities context)
        {
            return context.Messages.Where(p => p.RecievedUserID == userId ).ToList();
        }

        public List<Model.Message> GetUnreadMessages(int userId, ref Model.NutriLeafDBEntities context)
        {
            return context.Messages.Where(p => p.User.UserID==userId).ToList();
        }

        public List<Model.Message> GetSentMessages(int userId, DateTime datetime, ref Model.NutriLeafDBEntities context)
        {
            return context.Messages.Where(p => p.SentUserID == userId && p.DateTimeStamp==datetime) .ToList();
        }

        public List<Model.Message> GetReceivedMessages(int userId, DateTime datetime, ref Model.NutriLeafDBEntities context)
        {
            return context.Messages.Where(p => p.RecievedUserID == userId && p.DateTimeStamp == datetime).ToList();
        }

        public List<Model.Message> GetUnreadMessages(int userId, DateTime datetime, ref Model.NutriLeafDBEntities context)
        {
            return context.Messages.Where(p => p.RecievedUserID == userId && p.MessageRead == false).ToList();
        }

        public List<Model.Message> GetSentMessages(int userId, DateTime datetimeStart, DateTime datetimeEnd, ref Model.NutriLeafDBEntities context)
        {
            return context.Messages.Where(p => p.SentUserID==userId && p.DateTimeStamp>=datetimeStart && p.DateTimeStamp<datetimeEnd).ToList();
        }

        public List<Model.Message> GetReceivedMessages(int userId, DateTime datetimeStart, DateTime datetimeEnd, ref Model.NutriLeafDBEntities context)
        {
            return context.Messages.Where(p => p.RecievedUserID == userId && p.DateTimeStamp >= datetimeStart && p.DateTimeStamp < datetimeEnd).ToList();
        }

        public List<Model.Message> GetUnreadMessages(int userId, DateTime datetimeStart, DateTime datetimeEnd, ref Model.NutriLeafDBEntities context)
        {
            return context.Messages.Where(p => p.RecievedUserID== userId && p.DateTimeStamp >= datetimeStart && p.DateTimeStamp < datetimeEnd).ToList();
        }

        public void DeleteMessageThread(int messageId, ref Model.NutriLeafDBEntities context)
        {
           
            if (context.Messages.Where(p => p.MessageID==messageId).Count() != 0) {
                context.DeleteObject(context.Messages.Where(p => p.MessageID == messageId).First());
                context.SaveChanges();
            }
        }

        public void DeleteMessage(int messageId, ref Model.NutriLeafDBEntities context)
        {

            if (context.Messages.Where(p => p.MessageID == messageId).Count() != 0)
            {
                context.DeleteObject(context.Messages.Where(p => p.MessageID == messageId).First());
                context.SaveChanges();
            }
        }
    }
}